package com.sgb.kjwl.view.ui.fragment.home;

import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragEnterpriseConfigMenuBinding;
import com.swgk.core.view.entity.EnterpriseRecEntity;
import com.sgb.kjwl.view.ui.adapter.home.EnterpriseConfigMenuAdapter;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;

import java.util.ArrayList;
import java.util.List;

public class EnterpriseConfigMenuFragment extends AppFragment {

    public static final String KEY_DATA = "data";
    public static final String COLUMN_COUNT = "column_count";
    public static final String ROW_COUNT = "row_count";
    public static final String PAGE = "page";

    FragEnterpriseConfigMenuBinding binding;

    public static EnterpriseConfigMenuFragment newInstance(List<EnterpriseRecEntity> type, int column, int row, int page) {
        EnterpriseConfigMenuFragment fragment = new EnterpriseConfigMenuFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(KEY_DATA, (ArrayList<? extends Parcelable>) type);
        bundle.putInt(COLUMN_COUNT, column);
        bundle.putInt(ROW_COUNT, row);
        bundle.putInt(PAGE, page);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.frag_enterprise_config_menu, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    private void init() {
        List<EnterpriseRecEntity> data = getArguments().getParcelableArrayList(KEY_DATA);
        int column = (int) getArguments().get(COLUMN_COUNT);
        int row = (int) getArguments().get(ROW_COUNT);
        int page = (int) getArguments().get(PAGE);

        EnterpriseConfigMenuAdapter adapter = new EnterpriseConfigMenuAdapter(getActivity(), null);
        adapter.setListener(new EnterpriseConfigMenuAdapter.ItemEditClickListener() {
            @Override
            public void onItemDelete(EnterpriseRecEntity entity) {
                if (onItemClickCallBack != null) {
                    onItemClickCallBack.onClick(entity);
                }
            }
        });
        adapter.setCount(column * row);
        adapter.setDatas(data);
        binding.rlvMenu.setLayoutManager(new GridLayoutManager(getActivity(), column));
        binding.rlvMenu.addItemDecoration(new RecyclerCustomDecoration(
                getResources().getDimension(R.dimen.dp8),
                0,
                getResources().getDimension(R.dimen.dp8),
                0));
        binding.rlvMenu.setAdapter(adapter);
    }

    public OnItemClickCallBack onItemClickCallBack;

    public static abstract class OnItemClickCallBack {
        public void onClick(EnterpriseRecEntity entity) {
        }
    }

    public void setOnItemClickCallBack(OnItemClickCallBack onItemClickCallBack) {
        this.onItemClickCallBack = onItemClickCallBack;
    }
}
